package game;

import java.util.ArrayList;
import java.util.Vector;

import util.INIGroup;

public class Spell implements Loadable, Cloneable  {
	String tag;
	String opis;
	String ime;
	int manaCost;
	int lvlRequired;
	ArrayList<MagicEffect> efekti = new ArrayList<MagicEffect>();
	Vector<String> tagoviEfekata = null;
	boolean defensive = false;
	boolean targeted = true;

	public boolean isTargeted(){
		return targeted;
	}
	
	public boolean isDefensive(){
		return defensive;
	}
	
	public String getOpis() {
		return opis;
	}

	public void setOpis(String opis) {
		this.opis = opis;
	}

	public String getIme() {
		return ime;
	}

	public void setIme(String ime) {
		this.ime = ime;
	}

	public int getManaCost() {
		return manaCost;
	}

	public void setManaCost(int manaCost) {
		this.manaCost = manaCost;
	}

	public int getLvlRequired() {
		return lvlRequired;
	}

	public void setLvlReqrequired(int lvlRequired) {
		this.lvlRequired = lvlRequired;
	}

	public ArrayList<MagicEffect> getEfekti() {
		return efekti;
	}

	public void setEfekti(ArrayList<MagicEffect> efekti) {
		this.efekti = efekti;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String toString(){
		String ret;
		ret = "Spell: "+this.ime +" ("+this.lvlRequired+")\n";
		ret += "Opis: "+ this.opis+"\n";
		ret += "Odvaljiva "+this.manaCost+" mane\n";
		ret += "efekti: \n";
		for(MagicEffect e : this.efekti){
			ret += e+ "\n";
		}
		return ret;
	}
	
	@Override
	public boolean loadFromFile(INIGroup grupa) {
		this.tag = grupa.vals.get("tag");
		tagoviEfekata = new Vector<String>();
		tagoviEfekata.add(grupa.getString("efekat1"));
		tagoviEfekata.add(grupa.getString("efekat2"));
		tagoviEfekata.add(grupa.getString("efekat3"));
		tagoviEfekata.add(grupa.getString("efekat4"));
		tagoviEfekata.add(grupa.getString("efekat5"));
		this.ime = grupa.vals.get("ime");
		this.opis = grupa.getString("opis", "Nema opisa!");
		this.manaCost = grupa.getInteger("manaCost");
		this.lvlRequired = grupa.getInteger("lvlRequired");
		this.defensive = grupa.getBoolean("Defensive", false);
		this.targeted = grupa.getBoolean("Targeted", true);
		return false;
	}

	@Override
	public String getTag() {
		return this.tag;
	}
	
	public Spell clone() throws CloneNotSupportedException{
		Spell s = (Spell)(super.clone());
		s.efekti = new ArrayList<MagicEffect>();
		for(MagicEffect e : efekti)
			s.efekti.add(e.clone());
		return s;
	}

	@Override
	public void init() {
		for (String s : tagoviEfekata){
			if (!(s.equals(""))) efekti.add(GameBase.getEffectByTag(s));
		}
	}
	
	public boolean cast(Character caster, Character target){
		if (caster.getMana() < this.manaCost) return false;
		caster.setMana(caster.getMana() - this.manaCost);
		for (MagicEffect e : efekti){
			e.doEffect(caster, target);
		}
		
		return true;
	}
	

}
